package thread.demo06.CyclicBarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {
	private static final int THREAD_COUNT_NUM = 7;

	public static void main(String[] args) {

		// 设置第一个屏障点，等待召集齐7位法师
		CyclicBarrier callMasterBarrier = new CyclicBarrier(THREAD_COUNT_NUM, new Runnable() {
			@Override
			public void run() {
				/**
				 * 当CyclicBarrier完成后，下面只做一次
				 */
				System.out.println("7个法师召集完毕，同时出发，去往不同地方寻找龙珠！");
				summonDragon();
			}
		});

		// 召集齐7位法师
		for (int i = 1; i <= THREAD_COUNT_NUM; i++) {
			int index = i;
			new Thread(() -> {
				try {
					System.out.println("召集第" + index + "个法师");

					// callMasterBarrier减一
					callMasterBarrier.await();
				} catch (InterruptedException | BrokenBarrierException e) {
					e.printStackTrace();
				}
			}).start();
		}

		// ```
	}

	/**
	 * 召唤神龙：1、收集龙珠；2、召唤神龙
	 */
	private static void summonDragon() {

		// 设置第二个屏障点，等待7位法师收集完7颗龙珠，召唤神龙
		CyclicBarrier summonDragonBarrier = new CyclicBarrier(THREAD_COUNT_NUM, new Runnable() {
			@Override
			public void run() {
				System.out.println("集齐七颗龙珠！召唤神龙！");
			}
		});

		// 收集7颗龙珠
		for (int i = 1; i <= THREAD_COUNT_NUM; i++) {
			int index = i;

			// ..start
			new Thread(() -> {
				try {
					System.out.println("第" + index + "颗龙珠已收集到！");
					summonDragonBarrier.await();
				} catch (InterruptedException | BrokenBarrierException e) {
					e.printStackTrace();
				}
			}).start();
			// ..end
		}

		// ```
	}
}
